package com.ficus.road.maintain.controller.third;

import com.ficus.road.maintain.authority.dto.UserDTO;
import com.ficus.road.maintain.authority.service.UserService;
import com.ficus.road.maintain.authority.utils.JwtUtil;
import com.ficus.road.maintain.converter.SyncHitConverter;
import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.service.CarService;
import com.ficus.road.maintain.service.GpsHistoryService;
import com.ficus.road.maintain.service.HitService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ThirdController {
    @Autowired
    private HitService hitService;

    @Autowired
    private UserService userService;

    @Autowired
    private CarService carService;

    @Autowired
    private GpsHistoryService gpsHistoryService;

    @Autowired
    private SyncHitConverter syncHitConverter;

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("username", "zhouhao");
        params.add("password", "0192023a7bbd73250516f069df18b500");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.MULTIPART_FORM_DATA_VALUE));
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.exchange("http://139.224.227.138:9097/login", HttpMethod.POST, requestEntity, String.class);
        List<String> strings = stringResponseEntity.getHeaders().get("Set-Cookie");

        HttpHeaders headers1 = new HttpHeaders();
        headers1.add("Cookie", strings.get(0));
        HttpEntity request = new HttpEntity(headers1);
        ResponseEntity<String> stringResponseEntity1 = restTemplate.exchange("http://139.224.227.138:9097/company/coverRate/total", HttpMethod.GET, request, String.class);
        System.out.println("22");
    }

    @RequestMapping(value = "/hits/sync",
            method = RequestMethod.POST)
    public ResponseEntity<List<SyncHitResponse>> syncHits(@RequestParam(value = "start", required = false, defaultValue = "0") Integer start,
                                                          @RequestParam(value = "limit", required = false, defaultValue = "100") Integer limit,
                                                          @Valid @RequestBody SyncHitRequest syncHitRequest, @RequestHeader("Authorization") String authorization) {
        String username = JwtUtil.validateToken(authorization);
        UserDTO userDTO = userService.queryUserByUsername(username);
        Long endTime = syncHitRequest.getEndTime() != null ? syncHitRequest.getEndTime() : new Date().getTime() / 1000;
        Long startTime = syncHitRequest.getStartTime();
        List<Hit> hits = hitService.queryHits(start, limit, startTime, endTime, userDTO.getCarIdList());
        List<SyncHitResponse> syncHits = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(hits)) {
            syncHits = hits.stream().map(item -> syncHitConverter.convert(item)).collect(Collectors.toList());
        }
        return new ResponseEntity<>(syncHits, HttpStatus.OK);
    }

    /**
     * 获取车辆基本信息
     *
     * @return
     */
    @GetMapping("/cars")
    public ResponseEntity<List<CarBasicInfo>> getCarBasicInfo() {
        List<CarBasicInfo> basicInfo = carService.getBasicInfo();
        return new ResponseEntity<>(basicInfo, HttpStatus.OK);
    }

    /**
     * 获取车辆当前位置
     *
     * @return
     */
    @GetMapping("/cars/location")
    public ResponseEntity<List<CarLocation>> getCarsLocation() {
        List<CarLocation> carLocationList = carService.getCarLocation();
        return new ResponseEntity<>(carLocationList, HttpStatus.OK);
    }

    /**
     * 获取车辆出车记录
     *
     * @return
     */
    @PostMapping("/records/gps")
    public ResponseEntity<List<RecordGps>> getGpsRecords(@Valid @RequestBody RecordGpsRequest recordGpsRequest) {
        List<RecordGps> recordGpsList = gpsHistoryService.queryRecordGps(recordGpsRequest);
        return new ResponseEntity<>(recordGpsList, HttpStatus.OK);
    }
}
